package org.code.mallspringboot.config;

import org.code.mallspringboot.filter.JwtAuthenticationTokenFilter;
import org.code.mallspringboot.service.MemberService;
import org.code.mallspringboot.service.impl.MemberServiceImpl;
import org.code.mallspringboot.service.impl.UmsAdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * spring security 配置
 * @author jamie
 */
@Configuration
@EnableMethodSecurity
public class SecurityConfig {

    @Autowired
    MemberServiceImpl memberService;

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().requestMatchers("/sso/**","/member/**","/home/*","/product/**",
                "/member/**","/cart/**","/order/**");
    }

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {

        httpSecurity.authorizeHttpRequests(a->a
                .requestMatchers("/sso/**","/member/**","/home/**","/product/**",
                        "/member/**","/cart/**","/order/**").permitAll()
                .anyRequest().authenticated());

        httpSecurity.exceptionHandling(exceptionHandling -> exceptionHandling.accessDeniedHandler((
                ((request, response, accessDeniedException) -> {
                    response.setContentType("application/json;charset=utf-8");
                    response.getWriter().write("{\"code\":403;\"message\":\"权限不足\"}");
                    response.getWriter().flush();
                    response.getWriter().close();
                })
        )));

        httpSecurity.csrf(AbstractHttpConfigurer::disable);
        httpSecurity.cors(AbstractHttpConfigurer::disable);
        httpSecurity.sessionManagement(session->session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
        httpSecurity.userDetailsService(memberService);
        httpSecurity.addFilterBefore(new JwtAuthenticationTokenFilter(memberService), UsernamePasswordAuthenticationFilter.class);

        return httpSecurity.build();
    }

}
